package com.cskaoyan.controller.admin;

import com.cskaoyan.model.admin.bo.MarketCategoryBo;
import com.cskaoyan.model.admin.bo.MarketCreateCategoryBo;
import com.cskaoyan.model.admin.vo.*;
import com.cskaoyan.service.admin.AdminCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: 牛犇
 * @date 2021/11/29 19:31
 */
@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Autowired
    AdminCategoryService adminCategoryService;

    @RequestMapping("list")
    public BaseRespVo<BaseData1> list(){
        List<MarketCategoryVo> list=adminCategoryService.list();
        BaseData1 data = BaseData1.list(list, list.size());
        data.setLimit(list.size());
        data.setPages(1);
        data.setPage(1);
        return BaseRespVo.ok(data);
    }
    @RequestMapping("l1")
    public BaseRespVo<BaseData1> l1(){
        List<MarketCategoryL1Vo> list=adminCategoryService.list2();
        BaseData1 data = BaseData1.list(list, list.size());
        data.setLimit(list.size());
        data.setPages(1);
        data.setPage(1);
        return BaseRespVo.ok(data);
    }


    @PostMapping("update")
    public BaseRespVoNB update(@RequestBody MarketCategoryBo marketCategoryBo){
        String level=adminCategoryService.updateMarketCategory(marketCategoryBo);
        if(level.equals("L1")&&marketCategoryBo.getPid()==null){
            return BaseRespVoNB.fail(500,"父类参数不能为空");
        }
        return BaseRespVoNB.ok();
    }

    @PostMapping("delete")
    public BaseRespVoNB delete(@RequestBody  MarketCategoryBo marketCategoryBo){
        adminCategoryService.deleteMarketCategory(marketCategoryBo);
        return BaseRespVoNB.ok();
    }

    /**
     * 增加商品类目
     * @param marketCreateCategoryBo
     * @return
     */
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketCreateCategoryBo marketCreateCategoryBo){
        if(marketCreateCategoryBo.getPid()==0&&marketCreateCategoryBo.getLevel().equals("L2")){
            return BaseRespVo.fail(500,"父类目不能为空");
        }
        MarketCreateCategoryVo marketCreateCategoryVo=adminCategoryService.createMarketCategory(marketCreateCategoryBo);
        return  BaseRespVo.ok(marketCreateCategoryVo);
    }
}
